﻿/**
 * Copyright (c) 2008 michiyasu wada
 * http://www.seyself.com/
 * 
 * Distributed under The MIT License.
 * [http://www.opensource.org/licenses/mit-license.php]
 */

import flash.events.EventDispatcher;

class flash.events.Event
{
    
    public static function get ACTIVATE():String { return "activate"; }
    public static function get ADDED():String { return "added"; }
    public static function get ADDED_TO_STAGE():String { return "addedToStage"; }
    public static function get CANCEL():String { return "cancel"; }
    public static function get CHANGE():String { return "change"; }
    public static function get CLOSE ():String { return "close"; }
    public static function get COMPLETE():String { return "complete"; }
    public static function get CONNECT():String { return "connect"; }
    public static function get DEACTIVATE():String { return "deactivate"; }
    public static function get ENTER_FRAME ():String { return "enterFrame"; }
    public static function get FULLSCREEN():String { return "fullScreen"; }
    public static function get ID3():String { return "id3"; }
    public static function get INIT():String { return "init"; }
    public static function get MOUSE_LEAVE():String { return "mouseLeave"; }
    public static function get OPEN():String { return "open"; }
    public static function get REMOVED():String { return "removed"; }
    public static function get REMOVED_FROM_STAGE():String { return "removedFromStage"; }
    public static function get RENDER():String { return "render"; }
    public static function get RESIZE():String { return "resize"; }
    public static function get SCROLL():String { return "scroll"; }
    public static function get SELECT():String { return "select"; }
    public static function get SOUND_COMPLETE():String { return "soundComplete"; }
    public static function get TAB_CHILDREN_CHANGE():String { return "tabChildrenChange"; }
    public static function get TAB_ENABLED_CHANGE():String { return "tabEnabledChange"; }
    public static function get TAB_INDEX_CHANGE():String { return "tabIndexChange"; }
    public static function get UNLOAD():String { return "unload"; }
    
    public function get bubbles():Boolean { return _bubbles; }
    public function get cancelable():Boolean { return _cancelable; }
    public function get currentTarget():Object { return _currentTarget; }
    public function get eventPhase():Number { return _eventPhase; }
    public function get target():Object { return _target; }
    public function get type():String { return _type; }
    
    
    public function Event(type:String, bubbles:Boolean, cancelable:Boolean)
    {
        _bubbles = bubbles == true;
        _cancelable = cancelable == true;
        _type = type;
        _eventPhase = 0;
    }
    
    public function clone():Event
    {
        var e:Event = new Event( _type, _bubbles, _cancelable );
        e._eventPhase = _eventPhase;
        return e;
    }
    
    public function formatToString( className:String, args ):String
    {
        return _formatToSring.apply( this, arguments );
    }
    
    public function isDefaultPrevented():Boolean
    {
        return _defaultPrevented;
    }
    
    public function preventDefault():Void
    {
        if ( _cancelable ) _defaultPrevented = true;
    }
    
    public function stopImmediatePropagation():Void
    {
        _stopImmProp = true;
    }
    
    public function stopPropagation():Void
    {
        _stopProp = true;
    }
    
    public function toString():String
    {
        return _formatToSring("Event","type","bubbles","cancelable","eventPhase");
    }
    
    //--------------------------------------------------------------------------
    //
    //  PRIVATE
    //
    //--------------------------------------------------------------------------
    
    /**
     * @private
     * EventDispatcher が dispatchEvent 実行時に呼び出すメソッド
     */
    public function dispatchCall( target:Object, currentTarget:Object, phase:Number ):Event
    {
        if (arguments.caller == EventDispatcher.getEventObject) {
            var event:Event = this.clone();
            event._target = target;
            event._currentTarget = currentTarget;
            event._eventPhase = phase;
            return event;
        }
        return null;
    }
    
    private var _type : String;
    private var _bubbles : Boolean;
    private var _cancelable : Boolean;
    private var _eventPhase : Number = 0;
    private var _target : Object = null;
    private var _currentTarget : Object = null;
    private var _defaultPrevented : Boolean = false;
    private var _stopImmProp : Boolean = false;
    private var _stopProp : Boolean = false;
    
    private function _formatToSring( className, args ):String
    {
        var s:String = "[" + className;
        var len = arguments.length;
        for (var i:Number = 1; i < len; i++ ) {
            var val:String = arguments[i];
            s += " " + val + "=";
            if (typeof(this[val])=="string") {
                s += '"'+this[val]+'"';
            } else {
                s += this[val];
            }
        }
        return s + "]";
    }
    
}
